/**
 * 定时请求  将数组形式的 URL 组请求 使用window.result收集结果
 * @date 2020-08-18
 * @param {Array} arr 请求 URL 数组
 * @param {Number} msNum=500 请求间隔
 * @returns {Promise}
 */
async function requestTime(arr, options, msNum = 100) {
  return Promise.all(
    arr.map(
      (i, index) =>
        new Promise((resolve, reject) => {
          setTimeout(
            async (url, options) => {
              let res = await request(url, options);
              console.log(new Date().getTime());
              resolve(res);
            },
            index * msNum,
            arr[index],
            options
          );
        })
    )
  );
}

/**
 * fetch 请求封装
 * @date 2020-08-18
 * @param {Array} url
 * @param {Object} options
 * @returns {Promise}
 */
function request(url, options) {
  if (typeof url === "object") {
    options = url.options(options);
    url = url.url;
  }
  return new Promise((resolve, reject) => {
    fetch(url, options)
      .then((res) => res.text())
      .then((res) => resolve(res))
      .catch((err) => reject(err));
  });
}
